#include<stdio.h>
#define N 3

void swap(int *p1, int *p2)
{
  int tmp = *p1;
  *p1 = *p2;
  *p2 = tmp;
}

void copy(int *src, int *des, int len)
{
  int i;
  for (i = 0; i != len; ++i)
  {
    *des++ = *src++;
  }
}

void back_tracking(int ret[][N], int *pos_ret, int input_arr[], int path[], int pos_path, int n, int start_index)
{
  if (n == 0)
  {
    copy(path, ret[*pos_ret], N);
    ++*pos_ret;
  }
  else
  {
    int i;
    for (i = start_index; i < N; ++i)
    {
      swap(input_arr + start_index, input_arr + i);
      path[pos_path] = input_arr[start_index];
      //++start_index;
      //++pos_path;
      back_tracking(ret, pos_ret, input_arr, path, pos_path + 1, n - 1, start_index + 1);
      //--pos_path;
      //--start_index;
      swap(input_arr + start_index, input_arr + i);
    }
  }
}

int main()
{
  int ret[6][N];
  int input_arr[N] = {1, 2, 3};
  int path[N];
  int a = 0, *pa = &a;
  back_tracking(ret, pa, input_arr, path, 0, N, 0);

  int i, cnt = 6, j;
  for (i = 0; i < cnt; ++i)
  {
    for (j = 0; j < N; ++j)
    {
      printf("%d ", ret[i][j]);
    }
    printf("\n");
  }
  return 0;
}

